var express = require('express');
var session = require('express-session');
var router = express.Router();
var request = require('request');
var async = require('async');
var cookiesUtils = require('../utils/cookies-utils');

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: 'Express' });
});

router.get('/auth/login', function(req, res, next) {
    res.render('auth/login', { title: 'Express' });
});

router.post('/auth/doLogin', function(req, res, next) {
    console.log('req:' + JSON.stringify(req.body));
    var options = {
        url: 'http://localhost/api/auth/login',
        json: true,
        headers: {
            'Content-type': 'application/json;charset=UTF-8'
        },
        body: req.body
    };
    request.post(options, function (error, response, body) {
        console.log('error:' + error);
        console.log('response:' + JSON.stringify(response));
        console.log('body:' + JSON.stringify(body));
        if(error) {
            req.send(error);
        }
        if (!error && response.statusCode == 200) {
            console.log(body) // 打印google首页
            req.session.authen = {
                'isLogin': true,
                'user': 'hoosung'
            };
            //res.setHeader('Set-Cookie', cookiesUtils.serialize('isVisit', '1'));
            //res.render('login_success', { title: 'Express' });
            res.send(body);
        }
    });
    //res.render('login_success', { title: 'Express' });
});

router.get('/home', function(req, res, next) {
    res.render('admin/home', { title: 'Express' });
});

module.exports = router;
